package com.topcoder.srm514;

public class MagicalGirlLevelOneDivTwo {
	public double theMinDistance(int d, int x, int y) {
		if ((x - d) * (x + d) <= 0) {
			return (y - d) * (y + d) <= 0 ? 0 : y > d ? y - d : -d - y;
		}

		if ((y - d) * (y + d) <= 0)
			return x > d ? x - d : -d - x;

		double min = Math.sqrt((x - d) * (x - d) + (y - d) * (y - d));
		min = Math.min(min, Math.sqrt((x - d) * (x - d) + (y + d) * (y + d)));
		min = Math.min(min, Math.sqrt((x + d) * (x + d) + (y - d) * (y - d)));
		min = Math.min(min, Math.sqrt((x + d) * (x + d) + (y + d) * (y + d)));

		return min;
	}

	// online solution
	public double theMinDistance2(int d, int x, int y) {
		x = Math.abs(x);
		y = Math.abs(y);

		double amove = Math.max(0, y - d);
		double bmove = Math.max(0, x - d);

		return Math.sqrt(amove * amove + bmove * bmove);
	}
}
